/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.xy.biz.fd.domain.bo.DocumentBO;
import com.xy.biz.fd.domain.req.PrintQrCodeRequest;
import com.xy.biz.fd.domain.req.document.*;
import com.xy.biz.fd.domain.resp.GetEditorInfoResponse;
import com.xy.biz.fd.domain.resp.ListDocumentModifyInfoResponse;
import com.xy.lang.domain.Page;

import com.xy.lang.util.CalendarUtils;
import com.xy.tool.util.ConvertSizeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xy.biz.fd.domain.resp.GetDocumentResponse;
import com.xy.biz.fd.domain.query.DocumentQuery;
import com.xy.biz.fd.domain.converter.DocumentConverter;
import com.xy.biz.fd.client.DocumentClient;
import com.xy.biz.fd.service.DocumentService;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

/**
 *
 *
 * @author ssl
 * @date 2021-1-23
 */
@Api(tags = "文档服务")
@Slf4j
@RestController
@RequestMapping("/api/fd/document")
public class DocumentController implements DocumentClient {

    @Autowired
    private DocumentService documentService;

    @Override
    @ApiOperation(value = "保存文档")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<? extends GetDocumentResponse> saveDocument(@RequestBody SaveDocumentRequest request) {
        return documentService.saveDocument(DocumentConverter.toBO(request));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllDocument(@RequestBody Collection<SaveDocumentRequest> collection) {
        documentService.saveAllDocument(DocumentConverter.toBO(collection));
        return Result.success();
    }

    @Override
    @ApiOperation(value = "更新文档名")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<? extends Integer> updateDocument(@RequestBody SaveDocumentRequest request) {
        return documentService.updateDocument(DocumentConverter.toBO(request));
    }

    @ApiOperation(value = "替换文档")
    @PostMapping(value = "/replace")
    public Result<? extends Integer> replaceDocument(MultipartHttpServletRequest request) {
        return documentService.replaceDocument(request);
    }

    @Override
    public Result<?> updateAllDocument(@RequestBody Collection<SaveDocumentRequest> collection) {
        return documentService.updateAllDocument(DocumentConverter.toBO(collection));
    }

    @Override
    public Result<? extends Integer> updateDocumentByIds(@RequestBody SaveDocumentRequest request) {
        return documentService.updateDocumentById(DocumentConverter.toQuery(request));
    }

    @ApiOperation(value = "移动文档")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update_move")
    public Result<?> updateMoveDocument(@RequestBody SaveDocumentRequest request) {
        return documentService.updateMoveDocument(DocumentConverter.toBO(request));
    }

    @ApiOperation(value = "删除文档")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteDocument(@RequestBody GetDocumentRequest request) {
        return documentService.deleteDocument(request.getDocumentId());
    }

    @ApiOperation(value = "批量删除文档及文件夹")
    @Validatable
    @PostMapping(value = "/delete_by_ids")
    public Result<?> deleteDocumentByIds(@RequestBody GetDocumentChangeRequest request) {
        return documentService.deleteDocumentByIds(request);
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetDocumentResponse> getDocument(@RequestBody GetDocumentRequest request) {
        return Result.success(DocumentConverter.toResponse(documentService.getDocument(request.getDocumentId())));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetDocumentResponse>> listDocumentById(@RequestBody Collection<Long> collection) {
        return Result.success(DocumentConverter.toResponse(documentService.listDocumentById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetDocumentResponse>> listDocument(@RequestBody ListDocumentRequest request) {
        return Result.success(DocumentConverter.toResponse(documentService.listDocument(DocumentConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countDocument(@RequestBody ListDocumentRequest request) {
        return Result.success(documentService.countDocument(DocumentConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetDocumentResponse>> pageDocument(@RequestBody ListDocumentRequest request) {
        DocumentQuery query = DocumentConverter.toQuery(request);
        List<GetDocumentResponse> list = DocumentConverter.toResponse(documentService.listDocument(query));
        list = list.stream().peek(
                resp -> {
                    resp.setSize(ConvertSizeUtils.readableFileSize(resp.getFileSize()));
                    resp.setAccessTimeString(CalendarUtils.getDeadlineString(resp.getAccessTime(), LocalDateTime.now()));
                })
                .collect(Collectors.toList());
        int count = documentService.countDocument(query);
        return Result.success(new Page<>(list, count));
    }

    @ApiOperation(value = "新建文档")
    @PostMapping(value = "/create")
    public Result<?> createDocument(@RequestBody CreateDocumentRequest request) {
        GetDocumentResponse document = documentService.createDocument(DocumentConverter.toBO(request));
        return Result.success(document);
    }

    @ApiOperation(value = "上传文档")
    @PostMapping(value = "/upload")
    @Override
    public Result<?> uploadDocument(MultipartHttpServletRequest request) {
        return documentService.uploadDocument(request);
    }

    @Override
    @ApiOperation(value = "复制文档")
    @PostMapping(value = "/copy")
    public Result<GetDocumentResponse> copyDocument(@RequestBody CopyDocumentRequest request) {
        return documentService.copyDocument(request);
    }

    @ApiOperation(value = "恢复文档")
    @PostMapping(value = "/recover")
    public Result<?> createDocument(@RequestBody GetDocumentRequest request) {
        return documentService.recoverDocument(request.getDocumentId());
    }

    @ApiOperation(value = "批量恢复文档及文件夹")
    @PostMapping(value = "/recover_by_ids")
    public Result<?> createDocumentByIds(@RequestBody GetDocumentChangeRequest request) {
        return documentService.recoverDocumentByIds(request);
    }


    @ApiOperation(value = "获取文档信息-用于onlyOffice")
    @PostMapping(value = "/getDocument")
    public Result<?> getDocumentByDocumentId(@RequestBody GetDocumentRequest request) {
        GetEditorInfoResponse fileModel = documentService.getDocumentByDocument(request);
        return Result.success(fileModel);
    }


    @ApiOperation(value = "获取文档信息")
    @PostMapping(value = "/getDocumentInfo")
    public Result<?> getDocumentInfoByDocumentId(@RequestBody GetDocumentRequest request) {
        GetDocumentResponse documentResponse = documentService.getDocumentInfoByDocumentId(request.getDocumentId());
        return Result.success(documentResponse);
    }

    @ApiOperation(value = "获取文档历史变更信息")
    @PostMapping(value = "/listDocumentMotifyHistoryInfo")
    public Result<List<ListDocumentModifyInfoResponse>> listDocumentModifyHistoryInfoByDocumentId(@RequestBody ListDocumentModifyHistoryRequest request) {
        return documentService.listDocumentMotifyInfoByDocumentId(request.getDocumentId());
    }

    @ApiOperation(value = "根据文件名或文件编码模糊查询文件和文件夹")
    @PostMapping(value = "/fuzzySearchDocument")
    public Result<?> fuzzySearchDocumentByDocumentName(@RequestBody GetDocumentAndFolderRequest request) {
        return Result.success(documentService.listDocumentByDocumentName(request.getKeyword()));
    }

    @ApiOperation(value = "永久删除文件和文件夹")
    @PostMapping(value = "/real_delete")
    public Result<?> realDelete(@RequestBody GetDocumentChangeRequest request) {
        return documentService.realDelete(request);
    }

    @ApiOperation(value = "获取文件地址")
    @PostMapping(value = "/download_file")
    public Result<?> downloadFile(@RequestBody GetDocumentRequest request) {
        return documentService.downloadFile(request.getDocumentId());
    }

    @ApiOperation("二维码打印表格")
    @PostMapping(value = "/print_form")
    public Result<?> printForm(@RequestBody PrintQrCodeRequest request) throws IOException {
        return documentService.printForm(request);
    }
}
